package com.tarena.lbs.business.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.business.dao.mapper.StoreChainMapper;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.dao.repository.StoreChainRepository;
import com.tarena.lbs.business.pojo.param.StoreChainParam;
import com.tarena.lbs.business.pojo.po.StoreChainPO;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.StoreChainQuery;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository
public class StoreChainRepositoryImpl implements StoreChainRepository {
    @Autowired
    private StoreChainMapper storeChainMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Override
    public PageInfo<StoreChainPO> pageList(StoreChainQuery query) {
        List<StoreChainPO> poList = new ArrayList<>();
        // 使用PageHelper进行分页
        PageHelper.startPage(query.getPageNo(), query.getPageSize());

        //根据店铺的名称、区域查询出店铺信息，再通过店铺信息查出相关的二维列表
        if(query.getStoreName()==null && query.getArea()==null){
            QueryWrapper<StoreChainPO> queryWrapper = assmbleStoreQuery(query);
            poList = storeChainMapper.selectList(queryWrapper);
        }else{
            StoreQuery storeQuery = new StoreQuery();
            QueryWrapper<StorePO> queryWrapper = new QueryWrapper<>();
            if(StringUtils.isNotBlank(query.getStoreName())){
                storeQuery.setStoreName(query.getStoreName());
                queryWrapper.like("store_name",query.getStoreName());
            }
            if(query.getArea()!=null && query.getArea()>0){
                storeQuery.setAreaId(query.getArea());
                queryWrapper.eq("area_id",query.getArea());
            }
            List<StorePO> list = storeMapper.selectList(queryWrapper);
            List<Integer> storeIdList = new ArrayList<>();
            for (StorePO store : list){
                storeIdList.add(store.getId());
            }
            QueryWrapper<StoreChainPO> chainWrapper = new QueryWrapper<>();
            chainWrapper.in("store_id",storeIdList);
            poList = storeChainMapper.selectList(chainWrapper);
        }
        return new PageInfo<>(poList);
    }

    @Override
    public void update(StoreChainPO po){
        storeChainMapper.updateById(po);
    }

    @Override
    public void saveStoreChain(StoreChainPO po){
        storeChainMapper.insert(po);
    }

    /**
     * 将前端请求拼接成查询条件
     *
     * @param query
     * @return
     */
    private QueryWrapper<StoreChainPO> assmbleStoreQuery(StoreChainQuery query) {
        QueryWrapper<StoreChainPO> queryWrapper = new QueryWrapper<>();


        if(query.getBusinessId()!=null){
            queryWrapper.eq("business_id",query.getBusinessId());
        }

        queryWrapper.orderByDesc("create_time");
        queryWrapper.orderByDesc("id");

        return queryWrapper;
    }


}